package oleg; 

/**
 * Copyright 2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.pim.*;

import oleg.cfg.Config;
import oleg.locale.LocaleManager;
import oleg.rms.RMSManager;

public class WritingLangView
{
  private MainSearch mainSearch;
  private RMSManager rmsMgr = RMSManager.instance();

  private Form configForm = new Form("Writing Language");
  private ChoiceGroup langGroup = null;
  private Display display;
  private Displayable prevScreen;

  private Command backCommand = new Command("Back", Command.BACK, 2);

  String[] allLocales;

  public WritingLangView(MainSearch mainSearch) {
    this.mainSearch = mainSearch;
    display = Display.getDisplay(mainSearch);

    updateInterface();

    configForm.setCommandListener(new ViewCommandListener());
  }

  public void updateInterface() {
//     phonesList.removeCommand(exitCommand);
    configForm.removeCommand(backCommand);

    LocaleManager localeMgr = LocaleManager.instance();

    backCommand = new Command(localeMgr.getProperty(C.BACK), Command.BACK, 2);
    configForm.addCommand(backCommand);
    
    configForm.setTitle(localeMgr.getProperty(C.WRITING_LANGUAGE));

    configForm.deleteAll();
    initForm();
  }

  private void initForm() {
    LocaleManager localeMgr = LocaleManager.instance();
    allLocales = localeMgr.getAllLocaleNames();
    String[] humanAllLocales = new String[allLocales.length];
    for (int i = 0; i < allLocales.length; i++) {
      humanAllLocales[i] = localeMgr.getProperty(allLocales[i], C.HUMAN_LOCALE);
    }
    langGroup = new ChoiceGroup(localeMgr.getProperty(C.WRITING_LANGUAGE),
                                Choice.EXCLUSIVE,
                                humanAllLocales,
                                null);
    String curLocale = Config.instance().getWritingLang();
    for (int i = 0; i < allLocales.length; i++) {
      if (curLocale.equals(allLocales[i])) {
        langGroup.setSelectedIndex(i, true);
        break;
      }
    }

    configForm.append(langGroup);
  }

  public void showView() {
    prevScreen = display.getCurrent();
    display.setCurrent(configForm);
  }


  private class ViewCommandListener implements CommandListener
  {
    public void commandAction(Command c, Displayable s) {
//       if (c == exitCommand) {
//         mainSearch.exitApplication();
//         return;
//       }

      if (c == backCommand) {
        String lang = allLocales[langGroup.getSelectedIndex()];
        Config.instance().setWritingLang(lang);
        mainSearch.getMainForm().updateWritingLang();
        display.setCurrent(prevScreen);
        return;
      }

    }
  }

}
